package com.itwk.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwk.common.to.StockTo;
import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.Query;
import com.itwk.gulimall.ware.dao.WmsWareSkuDao;
import com.itwk.gulimall.ware.entity.WmsWareSkuEntity;
import com.itwk.gulimall.ware.service.WmsWareSkuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {

    //查询sku的库存信息 参数 skuId=&wareId=
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String skuId= (String) params.get("skuId");
        String wareId = (String) params.get("wareId");
        QueryWrapper<WmsWareSkuEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(skuId)){
            queryWrapper.eq("sku_id", skuId);
        }
        if(!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),
           queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<StockTo> hasStock(List<Long> skuIds) {

        List<StockTo> collect = skuIds.stream().map(skuId -> {
            StockTo stockTo = new StockTo();
            stockTo.setSkuId(skuId);
            stockTo.setHasStock(baseMapper.hasStock(skuId)==null?false:true);
            return stockTo;
        }).collect(Collectors.toList());

        return collect;
    }


}